package com.shangxia.lucky.lucky.job.write;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.shangxia.lucky.lucky.ConfigText;
import com.shangxia.lucky.lucky.common.FactoryHead;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

import static com.shangxia.lucky.lucky.job.write.MatchWriterThread.WRITER_OVER_LOG;


/**
 * @ProjectName: monomer
 * @ClassName: OverWriterThread
 * @Time: 2021/9/4
 * @Author: zhouy
 * @Description:
 * @Experience:
 */

@Slf4j
@Data
@NoArgsConstructor
public class CulomnWriterThread implements Runnable, ExportThreadJobOver {
    public CulomnWriterThread(ConfigText configText) {
        this.configText = configText;
    }

    public volatile static boolean CulomnWriterThread_OVER_LOG = false;

    public volatile static List<List<String>> CULOMNWRITERTHREAD_LISTS = Collections.synchronizedList(new ArrayList<>());

    @Autowired
    private ConfigText configText;

    @Override
    public void run() {
        log.info("输出 中号个数 列");
        String overFile = configText.getBananaAbsoluteResultPath() + DateFormatUtils.format(new Date(), "yyyy-MM-dd_H-mm-ss_中号个数") + ".xlsx";
        ExcelWriter excelWriter = EasyExcel.write(overFile).build();
        WriteSheet sheet = EasyExcel.writerSheet(0, "中号个数").head(FactoryHead.culomnHead(configText.getBananaExportColumn())).build();
        while (!WRITER_OVER_LOG || CULOMNWRITERTHREAD_LISTS.size() > 0) {
            if (CULOMNWRITERTHREAD_LISTS.size() == 0) {
                continue;
            }
            List<String> remove = CULOMNWRITERTHREAD_LISTS.remove(0);
            ArrayList<List<String>> list = new ArrayList();
            list.add(remove);
            excelWriter.write(list, sheet);
        }
        excelWriter.finish();
        log.info("输出 中号个数 列  结束");
        CulomnWriterThread_OVER_LOG = true;
    }


    @Override
    public Boolean over() {
        return null;
    }
}
